package com.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dto.OrdersAddForm;
import com.dto.OrdersSearchForm;
import com.entity.Material;
import com.entity.OrderDetail;
import com.entity.Orders;
import com.entity.Storage;
import com.service.*;
import com.utils.PageObject;
import com.vo.OrdersVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 *  入库订单前端控制器
 */
@Controller
@RequestMapping("/orders")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private SupplierService supplierService;

    @Autowired
    private StorageService storageService;

    @Autowired
    private MaterialService materialService;

    @Autowired
    private OrderDetailService orderDetailService;

    //分页查询
    @RequestMapping("/list")
    public String list(Model model, PageObject pageObject,  OrdersSearchForm form) {
        PageObject<OrdersVO> resultPage = ordersService.list(pageObject,form);
        model.addAttribute("page", resultPage);
        model.addAttribute("supplierList", supplierService.list());
        model.addAttribute("form", form);
        return "ordersList";
    }

    //批量删除
    @PostMapping("/batchDelete")
    @ResponseBody
    public String batchDelete(String orderNoArr){
        boolean batchDelete = this.ordersService.batchDelete(orderNoArr);
        if(batchDelete) return "success";
        return "fail";
    }

    //批量审核
    @PostMapping("/batchVerify")
    @ResponseBody
    public String batchVerify(String orderNoArr){
        boolean batchVerify = this.ordersService.batchVerify(orderNoArr);
        if(batchVerify) return "success";
        return "fail";
    }

    //批量作废
    @PostMapping("/batchInvalid")
    @ResponseBody
    public String batchInvalid(String orderNoArr){
        boolean batchVerify = this.ordersService.batchInvalid(orderNoArr);
        if(batchVerify) return "success";
        return "fail";
    }

    //新增页面初始化
    @GetMapping("/init")
    public String init(Model model){
        model.addAttribute("supplierList", this.supplierService.list());
        return "ordersAdd";
    }

    //获取仓库列表，供下拉框使用
    @GetMapping("/storageList")
    @ResponseBody
    public List<Storage> storageList(){
        return this.storageService.list();
    }

    //提供物料列表，供下拉框使用
    @GetMapping("/materialList")
    @ResponseBody
    public List<Material> materialList(){
        return this.materialService.list();
    }

    //添加功能前，判断添加订单的批号是否重复
    @PostMapping("/checkBatchNo")
    @ResponseBody
    public String checkBatchNo(String batchNoStr){
        boolean checkBatchNo = this.orderDetailService.checkBatchNo(batchNoStr);
        if(checkBatchNo) return "success";
        return "fail";
    }

    //订单添加
    @PostMapping("/save")
    @ResponseBody
    public String save(OrdersAddForm ordersAddForm){
        boolean save = this.ordersService.save(ordersAddForm);
        if(save) return "success";
        return "fail";
    }

    //单据详情，订单信息获取
    @GetMapping("/edit")
    public String edit(String orderNo,Model model){
        Orders orders = ordersService.selectByOrderNo(orderNo);
        model.addAttribute("orders", orders);
        model.addAttribute("supplierList", this.supplierService.list());
        return "ordersEdit";
    }

    //订单详情页面，修改订单信息
    @PostMapping("/update")
    @ResponseBody
    public String update(OrdersAddForm ordersAddForm){
        boolean save = this.ordersService.update(ordersAddForm);
        if(save) return "success";
        return "fail";
    }

    //订单详情-单个删除
    @PostMapping("/delete")
    @ResponseBody
    public String delete(String orderNo){
        boolean batchDelete = this.ordersService.delete(orderNo);
        if(batchDelete) return "success";
        return "fail";
    }

    //订单详情-单个审核
    @PostMapping("/verify")
    @ResponseBody
    public String verify(String orderNo){
        boolean batchVerify = this.ordersService.verify(orderNo);
        if(batchVerify) return "success";
        return "fail";
    }

    //订单详情-单个作废
    @PostMapping("/invalid")
    @ResponseBody
    public String invalid(String orderNo){
        boolean batchVerify = this.ordersService.invalid(orderNo);
        if(batchVerify) return "success";
        return "fail";
    }

    //采购退货单：分页查询
    @RequestMapping("/returnList")
    public String returnList(PageObject pageObject,Model model,OrdersSearchForm form){
        PageObject<OrdersVO> ordersVOPageObject = ordersService.ordersReturnList(pageObject, form);
        model.addAttribute("page", ordersVOPageObject);
        model.addAttribute("supplierList", supplierService.list());
        model.addAttribute("form", form);
        return "ordersReturnList";
    }

    //采购退货单：新增退货单
    @GetMapping("/returnInit")
    public String returnInit(Model model){
        model.addAttribute("supplierList", this.supplierService.list());
        return "ordersReturnAdd";
    }

    //采购退货单：退货明细下拉框
    @GetMapping("/ordersDetailList")
    @ResponseBody
    public List<OrderDetail> ordersDetailList(){
        return this.orderDetailService.orderDetailList();
    }

    //采购退货单：新增退货单
    @PostMapping("/return")
    @ResponseBody
    public String ordersReturn(OrdersAddForm ordersAddForm){
        boolean save = this.ordersService.ordersReturn(ordersAddForm);
        if(save) return "success";
        return "fail";
    }

}

